package com.handong.handserialprogram.module.setring;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.ColorContant;
import com.handong.handserialprogram.model.entities.FootRingNewEntity;
import com.handong.handserialprogram.model.status.FootRingStatus;
import com.handong.handserialprogram.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/21.
 */

public class RingListAdapter extends RecyclerView.Adapter<RingListAdapter.ViewHolder> {

    private static final int NORMAL_TEXT = Color.parseColor("#6198FE");
    private List<FootRingNewEntity> mFootRingList;
    private SetRingActivity mActivity;
    private RecyclerView mRecyclerView;

    public RingListAdapter(List<FootRingNewEntity> footRingList, SetRingActivity activity) {
        mFootRingList = footRingList;
        mActivity = activity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mRecyclerView = (RecyclerView) parent;

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_ring_list_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        FootRingNewEntity entity = mFootRingList.get(position);
        holder.mHolderEntity = entity;
        holder.ringID.setText(entity.getRingID());
        holder.ringActualDistance.setText(entity.getActualRingDistance() + "m");
        holder.ringSetDistance.setText(entity.getSetRingDistance() + "m");
        if (entity.getStatus() == FootRingStatus.ESCAPE) {
            holder.ringActualDistance.setTextColor(ColorContant.PERSON_ESCAPE);
        } else {
            holder.ringActualDistance.setTextColor(NORMAL_TEXT);
        }



      /*  FootRingNewEntity  bindingFootRing=mActivity.getCriminalEntity().getRingEntity();

        if(bindingFootRing!=null){
            if(entity.getRingID().equals(bindingFootRing.getRingID())){
                holder.mActivateLayout.setEnabled(true);
            }else  holder.mActivateLayout.setEnabled(false);
        }else  holder.mActivateLayout.setEnabled(true);*/


        if (entity.isBingding()) {
            //已绑定
            if (!entity.isMyBingding()) {
                setBtnState(holder, R.drawable.icon_ring_sleep, "已绑", Color.parseColor("#C0C0C0"), false);
            } else if (entity.isMyBingding() && (entity.getStatus() != FootRingStatus.NORMAL)) {
                setBtnState(holder, R.drawable.icon_ring_sleep, "关机", Color.parseColor("#C0C0C0"), false);
            } else if (entity.isMyBingding()) {
                if (entity.isNormalWork()) {
                    setBtnState(holder, R.drawable.icon_ring_working, "休眠", ColorContant.RING_CANCEL_BTN, true);
                } else {
                    if (entity.isResult()) {
                        if (entity.isAudioError()||entity.isSimError()||entity.isUwbError()||entity.isWifiError()||entity.isIs4gError())
                        {
                            setBtnState(holder, R.drawable.icon_ring_sleep, "异常", Color.parseColor("#FF0000"), true);
                        }
                        else{
                            setBtnState(holder, R.drawable.icon_ring_sleep, "激活", ColorContant.MISSION_NORMAL, true);
                        }

                    }
                    else{
                        setBtnState(holder, R.drawable.icon_ring_sleep, "自检" ,Color.parseColor("#C0C0C0"), false);
                    }
                }
            }
        } else {
            //未绑定
            if (entity.getStatus() != FootRingStatus.NORMAL) {
                setBtnState(holder, R.drawable.icon_ring_sleep, "关机", Color.parseColor("#C0C0C0"), false);
            } else if (entity.isNormalWork()) {
                setBtnState(holder, R.drawable.icon_ring_working, "休眠", ColorContant.RING_CANCEL_BTN, true);
            } else {
                // todo 一对一
//                boolean tag = false;
//                for (FootRingNewEntity footRingEntity : mFootRingList) {
//                    if (footRingEntity.isMyBingding()) {
//                        tag = true;
//                    }
//                }
//                if (tag) {
//                    setBtnState(holder, R.drawable.icon_ring_sleep, "激活", Color.parseColor("#C0C0C0"), false);
//                } else
//                    setBtnState(holder, R.drawable.icon_ring_sleep, "激活", ColorContant.MISSION_NORMAL, true);
                //todo 一对多
                if (entity.isResult()) {
                    if (entity.isAudioError()||entity.isSimError()||entity.isUwbError()||entity.isWifiError()||entity.isIs4gError())
                    {
                        setBtnState(holder, R.drawable.icon_ring_sleep, "异常", Color.parseColor("#FF0000"), true);
                    }
                    else{
                        setBtnState(holder, R.drawable.icon_ring_sleep, "激活", ColorContant.MISSION_NORMAL, true);
                    }

                }
                else{
                    setBtnState(holder, R.drawable.icon_ring_sleep, "自检" ,Color.parseColor("#C0C0C0"), false);
                }
            }
        }

    }

    private void setBtnState(ViewHolder holder, int iconID, String des, int color, boolean isEnabled) {
        GradientDrawable drawable = (GradientDrawable) holder.ringStatus.getBackground();
        holder.ringIcon.setImageResource(iconID);
        if (!TextUtils.isEmpty(des))
            holder.ringStatus.setText(des);
        drawable.setColor(color);
        holder.mActivateLayout.setEnabled(isEnabled);
    }

    @Override
    public int getItemCount() {
        return mFootRingList == null ? 0 : mFootRingList.size();
    }

    public View getView(int posstion) {
        ViewHolder holder = (ViewHolder) mRecyclerView.findViewHolderForLayoutPosition(posstion);
        return holder.itemView;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private FootRingNewEntity mHolderEntity;

        private TextView ringID, ringSetDistance, ringActualDistance, ringStatus;
        private FrameLayout mActivateLayout;
        private ImageView ringIcon;

        public ViewHolder(View itemView) {
            super(itemView);

            ringIcon = (ImageView) itemView.findViewById(R.id.ringItemIcon);
            ringID = (TextView) itemView.findViewById(R.id.ringItemID);
            ringSetDistance = (TextView) itemView.findViewById(R.id.ringItemSetDistance);
            ringActualDistance = (TextView) itemView.findViewById(R.id.ringItemActualDistance);
            ringStatus = (TextView) itemView.findViewById(R.id.ringItemBtnSet);
            mActivateLayout = (FrameLayout) ringStatus.getParent();
            mActivateLayout.setOnClickListener(this);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ringItemBtnSetLayout:
                    if (mHolderEntity.getStatus() == FootRingStatus.ESCAPE) {
                        AppUtil.showShortToast(mActivity, "无法检测到脚环");
                        return;
                    }
                    if (!mHolderEntity.isNormalWork()) {
                        if (mHolderEntity.isResult())
                        {
                            if(mHolderEntity.isWifiError()||mHolderEntity.isUwbError()||mHolderEntity.isSimError()||mHolderEntity.isAudioError()||mHolderEntity.isIs4gError())
                            {
                                mActivity.showCheckDialog(mHolderEntity.getRingID(),"\r\nWIFI模块:"+(mHolderEntity.isWifiError()?"异常":"正常")+"\r\n测距模块:"+(mHolderEntity.isUwbError()?"异常":"正常")+"\r\nSIM卡模块:"+(mHolderEntity.isSimError()?"异常":"正常")+"\r\n声音模块:"+(mHolderEntity.isAudioError()?"异常":"正常")+"\r\n4G模块:"+(mHolderEntity.isIs4gError()?"异常":"正常"));
                            }
                            else{
                                mActivity.activateFootRing(mHolderEntity.getRingID());
                            }
                        }
                        else{
                            AppUtil.showShortToast(mActivity, "脚环正在自检中！");
                        }
                    } else {
                        //休眠脚环
                        mActivity.showExitDialog(mHolderEntity.getRingID());
                    }
                    break;

                case R.id.ringItemContainer:
                    if (mHolderEntity.isResult())
                    {
                        if(mHolderEntity.isWifiError()||mHolderEntity.isUwbError()||mHolderEntity.isSimError()||mHolderEntity.isAudioError()||mHolderEntity.isIs4gError())
                        {
                            mActivity.showCheckDialog(mHolderEntity.getRingID(),"\r\nWIFI模块:"+(mHolderEntity.isWifiError()?"异常":"正常")+"\r\n测距模块:"+(mHolderEntity.isUwbError()?"异常":"正常")+"\r\nSIM卡模块:"+(mHolderEntity.isSimError()?"异常":"正常")+"\r\n声音模块:"+(mHolderEntity.isAudioError()?"异常":"正常")+"\r\n4G模块:"+(mHolderEntity.isIs4gError()?"异常":"正常"));
                        }
                        else {
                            if (!mHolderEntity.isNormalWork()) {
                                //激活脚环引导
                                mActivity.showActivateGuideView(ringStatus, R.id.ringItemBtnSet);
                                return;
                            }
                            mActivity.uploadPersonBindInfo(mHolderEntity);
                        }
                    }
                    else{
                        AppUtil.showShortToast(mActivity, "脚环正在自检中！");
                    }
                    break;
            }
        }


    }


}
